<?php
class Hydrum_Theme
{
    /**
     * @var Zend_View
     */
    protected $view;

    /**
     * @var Zend_Layout
     */
    protected $layout;

    /**
     * @var Hydrum_Theme_Skin
     */
    protected $skin;

    const DEFAULT_DOCTYPE = Zend_View_Helper_Doctype::XHTML1_TRANSITIONAL;

    /**
     * @var array
     */
    protected $options;

    public function __construct(array $options = array())
    {
        $this->setOptions($options);

        $this->initView()->initLayout() //  ->initSkin()
        ->initPaginator();
    }

    public function setOptions(array $options)
    {
        $this->options = $options;
    }

    public function getPaths($path = '')
    {
        $options = Hydrum::getApplication()->getOptions();

        $applications = $options['applications'];

        $paths = array();

        foreach ($applications as $basePath) {
            $paths[] = $basePath . DS . 'themes' . DS . $this->options['name'] . DS . $path;
        }

        return $paths;
    }

    protected function initView()
    {
        if ($this->view == null) {
            $this->view = new Zend_View();

            $this->view->addScriptPath($this->getPaths('scripts'));

            $this->view->addScriptPath($this->getPaths('partials'));

            $this->view->addHelperPath(LIBRARIES_PATH . '/Hydrum/View/Helper', 'Hydrum_View_Helper_');

            $this->view->doctype($this->getDoctype());

            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');

            $viewRenderer->setViewScriptPathSpec(':module/:controller/:action.:suffix');
            $viewRenderer->setView($this->view);
        }

        return $this;
    }

    protected function getDoctype()
    {
        if (!isset($this->options['doctype'])) {
            $this->options['doctype'] = self::DEFAULT_DOCTYPE;
        }

        return $this->options['doctype'];
    }

    protected function initLayout()
    {
        if ($this->layout == null) {
            $this->layout = Zend_Layout::startMvc(array(
                'layout'     => $this->options['layout']['name'],
                'layoutPath' => $this->getPaths('layouts'),
                'view'       => $this->getView()
            ));
        }

        return $this;
    }

    protected function initSkin()
    {
        if (is_null($this->skin )) {
            $this->skin = new Hydrum_Theme_Skin($this->options['skin'], $this->options['name']);
        }

        return $this;
    }

    protected function initPaginator()
    {
        Zend_Paginator::setDefaultScrollingStyle('Sliding');
        Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');

        return $this;
    }

    public function getSkin($name = null)
    {
        $skin = $this->skin;

        if ($name != null)
            $skin = new Hydrum_Theme_Skin($name, $this->options['name']);

        return $skin;
    }

    public function getView()
    {
        return $this->view;
    }

    public function getLayout()
    {
        return $this->layout;
    }
}
